package com.alex.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;

public class MyUtils {

    public static final Map<String, String> map = new HashMap<>();

    // 全局数组
    private final static String[] strDigits = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    static {
        map.put("黑色", "30");
        map.put("红色", "31");
        map.put("绿色", "32");
        map.put("黄色", "33");
        map.put("蓝色", "34");
        map.put("紫红色", "35");
        map.put("青蓝色", "36");
        map.put("白色", "37");
    }

    public static String GetMD5Code(String strObj) {
        String resultString = null;
        if (StringUtil.isNotNull(strObj)) {
            try {
                resultString = new String(strObj);
                MessageDigest md = MessageDigest.getInstance("MD5");
                // md.digest() 该函数返回值为存放哈希值结果的byte数组
                resultString = byteToString(md.digest(strObj.getBytes()));
            } catch (NoSuchAlgorithmException ex) {
                ex.printStackTrace();
            }
        }

        return resultString;
    }

    // 获取此路径下全部图片文件
    public static List<File> getExcelAll(String path) {
        List<File> fLs = new ArrayList<>();
        List<File> collect = new ArrayList<>();
        if (FileUtil.exist(path)) {
            File[] ls = FileUtil.ls(path);
            for (File l : ls) {
                if (l.getName().endsWith(".xls") || l.getName().endsWith(".xlsx")) {
                    fLs.add(l);
                }
            }
            System.out.println("路径下+" + path + "+excel数量++" + fLs.size());
            collect = fLs.stream().sorted(Comparator.comparing(String::valueOf)).collect(Collectors.toList());
        }


        return collect;
    }

    // 获取此路径下全部图片文件
    public static List<File> getImageAll(String path) {
        List<File> fLs = new ArrayList<>();
        List<File> collect = new ArrayList<>();
        if (FileUtil.exist(path)) {
            File[] ls = FileUtil.ls(path);
            for (File l : ls) {
                if (l.getName().endsWith(".png") || l.getName().endsWith(".jpeg") || l.getName().endsWith(".tiff")) {
                    fLs.add(l);
                }
            }
            System.out.println("路径下+" + path + "+图片数量++" + fLs.size());
            collect = fLs.stream().sorted(Comparator.comparing(String::valueOf)).collect(Collectors.toList());
        }


        return collect;
    }


    // 转换字节数组为16进制字串
    private static String byteToString(byte[] bByte) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < bByte.length; i++) {
            sBuffer.append(byteToArrayString(bByte[i]));
        }
        return sBuffer.toString();
    }

    // 返回形式为数字跟字符串
    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        // System.out.println("iRet="+iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }


    public static void zsout(Object string) {
        System.out.println("\033[1;" + "30" + "m" + DateUtil.date() + "\033[0m" + "\033[1;" + "31" + "m" + string + "\033[0m \n");
    }
    public static void zsout(Object string, boolean printLog) {
        if (printLog) {
            System.out.println("\033[1;" + "30" + "m" + DateUtil.date() + "\033[0m" + "\033[1;" + "31" + "m" + string + "\033[0m \n");
        }
    }

    public static void zsout(Object string, String color) {

        System.out.println("\033[1;" + "30" + "m" + DateUtil.date() + "\033[0m" + "\033[1;" + map.get(color) + "m" + string + "\033[0m \n");
    }

    public static void zsout(Object string, String color, Boolean printLog) {
        System.out.println("\033[1;" + "30" + "m" + DateUtil.date() + "\033[0m" + "\033[1;" + map.get(color) + "m" + string + "\033[0m \n");
    }
}
